package cn.cloud9.activiti7.uelexpression;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * 使用UEL表达式分配负责人
 * @author OnCloud9
 * @description
 * @project activiti7
 * @date 2022年08月20日 下午 03:40
 */
public class UelExpression {
    private ProcessEngine processEngine;
    private RepositoryService repositoryService;
    private RuntimeService runtimeService;
    private TaskService taskService;
    private HistoryService historyService;
    private ManagementService managementService;

    // key对应流程设计时的表达式key ${assignee0}, ${assignee1}, ${assignee2}, ${assignee3}
    private static final Map<String, Object> ASSIGNEE_MAP = new HashMap<String, Object>() {{
        this.put("assignee0", "张三");
        this.put("assignee1", "李四");
        this.put("assignee2", "王五");
        this.put("assignee3", "赵六");
    }};

    /**
     * 初始化预备资源
     */
    @Before
    public void loadingResource() {
        processEngine = ProcessEngines.getDefaultProcessEngine();
        repositoryService = processEngine.getRepositoryService();
        runtimeService = processEngine.getRuntimeService();
        taskService = processEngine.getTaskService();
        historyService = processEngine.getHistoryService();
        managementService = processEngine.getManagementService();
    }

    @Test
    public void deploymentProcess() {
        final Deployment deploy = repositoryService.createDeployment()
                .name("出差流程申请-UEL表达式实现")
                .addClasspathResource("bpmn/evection-uel.png")
                .addClasspathResource("bpmn/evection-uel.bpmn20.xml")
                .deploy();

        final String id = deploy.getId();
        final String name = deploy.getName();
        System.out.println("id = " + id);
        System.out.println("name = " + name);
    }

    @Test
    public void startProcessInstance() {

        final ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(
                "MyEvection2",
                ASSIGNEE_MAP
        );

        final String deploymentId = processInstance.getDeploymentId();
        final String processDefinitionId = processInstance.getProcessDefinitionId();
        final String activityId = processInstance.getActivityId();

        System.out.println("流程部署ID deploymentId = " + deploymentId);
        System.out.println("流程定义ID processDefinitionId = " + processDefinitionId);
        System.out.println("流程活动ID activityId = " + activityId);
    }
}
